package chen.web.controller.forum.model;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import chen.web.MyContextProvider;
import chen.web.forum.Category;
import chen.web.forum.CategoryManager;
import chen.web.forum.ForumAccount;
import chen.web.forum.ReplyCondition;
import chen.web.forum.SubjectCondition;
import chen.web.forum.PostAmountLevel;
import chen.web.forum.SubjectCondition.Order;
import chen.web.forum.SubjectCondition.OrderBy;
import chen.web.forum.TopicInfo;
import chen.web.forum.TopicManager;
import chen.web.forum.SubjectCondition.SubjectType;
import chen.web.message.MessageManager;
import chen.web.user.Account;
import chen.web.user.AccountManager;

public class Model {

	@Autowired
	private CategoryManager cm;
	@Autowired
	private AccountManager am;
	@Autowired
	private TopicManager tm;
	@Autowired
	private MessageManager mm;
	
	public Account getCUA(){
		return MyContextProvider.getMyContext().getAccount();
	}
	
	public Category getRootCategory(){
		return cm.getRootCategory();
	}
	
	public int getTotalUserCount(){
		return am.totalAccount();
	}
	
	public List<TopicInfo> topTopicList(Category category){
		List<TopicInfo> topicInfoList = tm.getTopicInfoList(new SubjectCondition().setCategory(category).setContainsChildren(true).setType(SubjectType.top), 0, Integer.MAX_VALUE);
		return topicInfoList;
	}
	
	public int totalSubjectCount(Category category){
		int subjectAmount = tm.totalSubject(new SubjectCondition().setCategory(category).setContainsChildren(true));
		return subjectAmount;
	}
	
	public int totalReplyCount(Category category){
		ReplyCondition condition = new ReplyCondition().setCategory(category).setContainsChildren(true);
		int replyAmount = tm.totalReply(condition);
		return replyAmount;
	}
	
	public int getNewMessageCount(){
		return mm.newMessageCount(MyContextProvider.getMyContext().getAccount());
	}
	
	public String postLevelName(ForumAccount account){
		return PostAmountLevel.getLevel(account.getReplyCount()+account.getSubjectCount()).name();
	}
	
	public int postLevelOrdinal(ForumAccount account){
		int ordinal = PostAmountLevel.getLevel(account.getReplyCount()+account.getSubjectCount()).ordinal();
		return ordinal;
	}
	
/*	*//**
	 * 得到当前栏目可以发布的栏目,约定只有处于最底层的栏目才可以发布内容
	 * @param category
	 * @return
	 *//*
	public List<Category> postableCategory(Category category){
		//
		if(category.getEldestSon() == null){
			return Collections.singletonList(category);
		}
		List<Category> children =  category.getChildren();
		for(int i=0; i<children.size(); i++){
			if(children.get(i).getEldestSon()!= null){
				children.remove(i);
			}
		}
		return children;
	}*/
	
	/**
	 * 得到当前栏目的上级栏目. 顺序是由根栏目开始, 一直到当前栏目, 包含当前栏目
	 */
	public List<Category> higherLevelList(Category category){
		List<Category> list = new ArrayList<Category>();
		Category parent = category.getParent();
		while(parent != null){
			list.add(parent);
			parent = parent.getParent();
		}
		int n = list.size() / 2;
		//颠倒顺序
		for(int i=0; i<n; i++){
			Category temp = list.get(i);
			int index = list.size()-1-i;
			list.set(i, list.get(index));
			list.set(index, temp);
		}
		//包含当前栏目
		list.add(category);
		return list;
	}
	
	private Region region = new Region();
	public Region getRegion(){
		return region;
	}
	
	//最新
	public List<TopicInfo> latestTopic(Category category, int count){
		return tm.getTopicInfoList(new SubjectCondition().setCategory(category).setOrderBy(OrderBy.PublishDate).setOrder(Order.Desc), 0, count);
	}
	
	//按最后回复
	public List<TopicInfo> topic(Category category, int count){
		return tm.getTopicInfoList(new SubjectCondition().setCategory(category).setOrderBy(OrderBy.LastReplyDate).setOrder(Order.Desc), 0, count);
	}
	
	//精华
	public List<TopicInfo> digestTopic(Category category, int count){
		return tm.getTopicInfoList(new SubjectCondition().setCategory(category).setType(SubjectType.good).setOrderBy(OrderBy.LastReplyDate).setOrder(Order.Desc), 0, count);
	}
	
	private Permission permission = null;
	public Permission getPermission(){
		if(permission == null){
			permission = new Permission(cm);
		}
		return permission;
	}
}
